/*
 * @Author: Shiire
 * @Date: 2022-02-22 19:15:43
 * @LastEditors: Shiire
 * @LastEditTime: 2022-08-20 20:34:40
 * @Description: 
 * @FilePath: \grapro\src\utils\instance.js
 */
import axios from 'axios'

// axios.defaults.baseURL = "http://127.0.0.1:8080/api"
// axios.defaults.baseURL = "http://42.192.69.157/api"
axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? 'http://127.0.0.1:8080/api' : 'http://42.192.69.157/api'


// axios.defaults.transformRequest = (data, headers) =>{
//     const contentType = headers['Content-Type']
//     if(contentType === 'application/x-www-form-urlencoded') return qs.stringify(data)

//     return data
// }

axios.interceptors.request.use(config => {
    config.headers.Authorization = window.localStorage.getItem('token')
    return config
})

axios.interceptors.response.use(function (response) {
    if (response.data.status === 401) {
        window.localStorage.removeItem('token');
        router.replace({
            path: '/login' // 到登录页重新获取token
        })
    }

    return response
}, function (error) {
    return this.$message.error(error)
})

export default axios